﻿using RpcCentral.Common;
using RpcCentral.Model;
using RpcCentral.Model.DB;
using SqlSugarHelper;

namespace RpcCentral.DAL.Repository
{
    internal class ServerRunStateDAL : IServerRunStateDAL
    {
        private IRepository<ServerRunStateModel> _Db;
        public ServerRunStateDAL(IRepository<ServerRunStateModel> db)
        {
            _Db = db;
        }
        public bool SetServiceState(ServiceRunState[] states)
        {
            ServerRunStateModel[] models = states.ConvertMap<ServiceRunState, ServerRunStateModel>();
            return _Db.UpdateOnly(models, a =>new  { 
                a.ConNum,
                a.CpuRunTime,
                a.GCBody,
                a.LockContentionCount,
                a.MachineName,
                a.SyncTime,
                a.ThreadNum,
                a.WorkMemory
            });
        }
        public RunEnvironment GetEnvironments(long serverId)
        {
            return this._Db.Get(a =>a.ServerId==serverId,c=>new RunEnvironment { 
                RunIsAdmin=c.RunIsAdmin,
                RunUserIdentity=c.RunUserIdentity
            });
        }
        public bool CheckIsReg(long id)
        {
            return _Db.IsExist(c => c.ServerId == id, c => c.ServerId);
        }
        public void AddRunState(ServerRunStateModel add)
        {
            this._Db.Insert(add);
        }
        public bool UpdateRunState(long serverId, ServerRunStateSet datum)
        {
            return _Db.Update<ServerRunStateSet>(datum, a => a.ServerId == serverId);
        }
    }
}
